<template>
	<view class="" style="width: 100vw;height: 100vh">
		<hx-navbar title="我的" :back="true" :fixedWhiteStyle="true" :fixed="false" :color="kblackColor"
			barPlaceholder="hidden" transparent="auto">
		</hx-navbar>
		<view class="container" style="min-height: 100vh;background:white;">

			<view class="header">
				<view class="item" @click="pushDiamond()">
					<image class="item-icon" src="@/static/aipicture/ai-diamond-icon.png"></image>
					<text class="item-title">灵感星</text>
				</view>
				<view class="item" @click="pushVote()">
					<image class="item-icon" src="@/static/aipicture/ai-vote-icon.png"></image>
					<text class="item-title">投票明细</text>
				</view>
				<view class="item" @click="pushAward()">
					<image class="item-icon" src="@/static/aipicture/ai-award-icon.png"></image>
					<text class="item-title">我的奖品</text>
				</view>
			</view>
			<view class="title-row">
				<image class="title-icon" src="@/static/aipicture/ai-child-title-icon.png"></image>
				<text class="title">我的创作</text>
			</view>
			<view class="content-list">
				<view class="item" v-for="item in list">
					<image class="cover" :src="item.worksImage"></image>
					<view class="text">权重票：
						<text v-if="item.ticketTotal=='' || item.ticketTotal==null">0</text>
						<text v-else>{{ item.ticketTotal }}</text>
					</view>
					<view class="text" @click="copySn(item.sn)">比赛场次：
						{{ item.sn }}
					</view>
					<view class="save-btn" @click="saveImage(item.worksImage)">保存作品</view>
				</view>
			</view>
			<view class="none_box" v-if="list.length == 0">
				<image src="@/static/without.png" mode="widthFix"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				hasNextPage: true,
				pageNo: 1,
				pageSize: 10,
			}
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			if(this.hasNextPage){
				this.pageNo = this.pageNo + 1;
				this.getList()
			}
		},
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
			this.pageNo =1
			this.list =[]
			this.getList()
		},
		methods: {
			getList() {
				this.$api.aiEventWorkslist(this.pageNo, (res) => {
					if (res.status) {
						this.list = this.list.concat(res.data.list)
						this.hasNextPage = res.data.list.length >= this.pageSize;
					}

				})
			},
			copySn(val){
				let that = this
				if (!val) {
					this.$common.errorToShow('未获取场次编号');
					return;
				}
				uni.setClipboardData({
					data: val,
					success() {
						that.$common.errorToShow('已复制剪贴板')
					},
					fail(r) {
						that.$common.errorToShow(r)
					}
				})
			},
			saveImage(e) {
				
				//e 图片地址
				// 非H5环境中
				// #ifndef H5
				uni.saveImageToPhotosAlbum({
					filePath: e, //图片url
					success: () => {
						uni.showToast({
							title: "保存成功",
							icon: "success"
						})
					},
					fail: function(err) {
						 uni.showToast({
						 title: '图片保存失败,请检查app是否开启照片权限',
						 	icon: 'none'
						 });
						 console.error(err);
					}
				})
				console.log("--",e);
				// #endif
				//H5环境中，也可以提示长按屏幕保存，让用户操作，不执行以下事件
				// #ifdef H5
					if (typeof plus !== "undefined"){
						// 当前运行在H5+环境中
						var down = plus.downloader.createDownload(e, {}, function(e, a) {
							plus.gallery.save(e.filename,function (e) {
							uni.showToast({
								title: "保存成功",
								mask:true									,
							});	
							console.log("下载成功");
							},function (e) {
								uni.showToast({
								title: '图片保存失败',
									icon: 'none'
								});
							})
						});
						down.start();
					}else{
						var oA = document.createElement("a");
						oA.download = '作品.png'; // 设置下载的文件名，默认是'下载'
						oA.href = e; //图片url
						document.body.appendChild(oA);
						oA.click();
						oA.remove(); // 下载之后把创建的元素删除
					}	
				// #endif
			},
			pushDiamond() {
				uni.navigateTo({
					url: '/pages/aipicture/mine/diamond-list'
				})
			},
			pushVote() {
				uni.navigateTo({
					url: '/pages/aipicture/mine/vote-list'
				})
			},
			pushAward() {
				uni.navigateTo({
					url: '/pages/aipicture/mine/award-list'
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 40rpx;
	}

	.header {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 28rpx 80rpx;
		border-radius: 10rpx;
		background: linear-gradient(135deg, #6C58D5 0%, #AA56D7 100%);

		.item {
			display: flex;
			flex-direction: column;
			align-items: center;

			.item-icon {
				width: 88rpx;
				height: 88rpx;
			}

			.item-title {
				margin-top: 15rpx;
				color: #FFFFFF;
				font-size: 24rpx;
			}
		}
	}

	.title-row {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin: 30rpx 0;

		.title-icon {
			width: 40rpx;
			height: 40rpx;
		}

		.title {
			margin-left: 20rpx;
			font-size: 30rpx;
			font-weight: bold;
			color: black;
		}
	}

	.content-list {
		width: 100%;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		justify-content: space-between;

		.item {
			width: 48%;
			// height: 480rpx;
			border-radius: 20rpx;
			overflow: hidden;
			margin-bottom: 30rpx;
			background-color: #FFFFFF;
			box-shadow: 0rpx 9rpx 18rpx 0rpx rgba(156, 156, 156, 0.6);
			position: relative;

			.cover {
				width: 100%;
				height: 310rpx;
				position: relative;
				z-index: 1;
				margin-bottom: 20rpx;
			}

			.text {
				margin: 0 20rpx 0rpx 20rpx;
				color: #010101;
				font-size: 24rpx;
			}

			.save-btn {
				margin: 10rpx 20rpx 20rpx 20rpx;
				color: white;
				width: 160rpx;
				height: 50rpx;
				line-height: 50rpx;
				font-size: 28rpx;
				border-radius: 25rpx;
				text-align: center;
				background: linear-gradient(135deg, #6C58D5 0%, #AA56D7 100%);
			}
		}
	}

	.none_box {
		width: 534rpx;
		margin: 117rpx auto;
		font-size: 32rpx;
		color: #ABA9B4;
	}

	.none_box image {
		width: 100%;
		height: 534rpx;
		margin-bottom: 10rpx;
	}
</style>